package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.UacGroupIpAddr;
import com.csun.cmny.provider.model.domain.UacGroupIpSegment;
import com.csun.cmny.provider.model.domain.UacGroupIpUser;
import com.csun.cmny.provider.model.vo.ipauth.GroupIpAuthVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.UacGroupIpAddrService;
import com.csun.cmny.provider.service.UacGroupIpSegmentService;
import com.csun.cmny.provider.service.UacGroupIpUserService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import io.swagger.annotations.Api;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2020-09-30 14:18
 * @Description:
 */
@RestController
@RequestMapping(value = "/ip", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(value = "Web - UacGroupCommonController", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class UacGroupIpController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private UacGroupIpAddrService uacGroupIpAddrService;
    @Resource
    private UacGroupIpSegmentService uacGroupIpSegmentService;
    @Resource
    private UacGroupIpUserService uacGroupIpUserService;

    /**
     * 查询
     */
    @PostMapping(value = "/getIpAuth")
    public Wrapper getIpAuth() {

        GroupIpAuthVo groupIpAuthVo = new GroupIpAuthVo();

        Long groupId = getLoginAuthDto().getGroupId();
        groupIpAuthVo.setIpAddrVoList(uacGroupIpAddrService.selectByGroupId(groupId));
        groupIpAuthVo.setIpSegmentVoList(uacGroupIpSegmentService.selectByGroupId(groupId));
        groupIpAuthVo.setIpUserVoList(uacGroupIpUserService.selectByGroupId(groupId));

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), groupIpAuthVo);
    }

    /**
     * 保存
     */
    @PostMapping(value = "/setIpAuth")
    public Wrapper setIpAuth(@RequestBody GroupIpAuthVo groupIpAuthVo) {

        Integer result = 0;
        result += uacGroupIpAddrService.setIpAuth(groupIpAuthVo.getIpAddrVoList(), getLoginAuthDto());
        result += uacGroupIpSegmentService.setIpAuth(groupIpAuthVo.getIpSegmentVoList(), getLoginAuthDto());
        result += uacGroupIpUserService.setIpAuth(groupIpAuthVo.getIpUserVoList(), getLoginAuthDto());

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 删除
     */
    @PostMapping(value = "/deleteIpAuth")
    public Wrapper deleteIpAuth() {

        Long groupId = getLoginAuthDto().getGroupId();
        Integer result = 0;
        result += uacGroupIpAddrService.deleteByGroupId(groupId);
        result += uacGroupIpSegmentService.deleteByGroupId(groupId);
        result += uacGroupIpUserService.deleteByGroupId(groupId);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }


    /**
     * 保存ip地址设置
     */
    @PostMapping(value = "/saveIpAddr")
    public Wrapper saveIpAddr(@RequestBody UacGroupIpAddr uacGroupIpAddr) {

        Integer result = uacGroupIpAddrService.save(uacGroupIpAddr, getLoginAuthDto());

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 根据Id删除ip地址设置
     */
    @PostMapping(value = "/deleteIpAddrById/{id}")
    public Wrapper deleteIpAddrById(@PathVariable Long id) {

        Integer result = uacGroupIpAddrService.deleteByKey(id);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 查询ip地址设置
     */
    @PostMapping(value = "/queryIpAddrList")
    public Wrapper queryIpAddrList() {

        UacGroupIpAddr uacGroupIpAddr = new UacGroupIpAddr();
        uacGroupIpAddr.setGroupId(getLoginAuthDto().getGroupId());
        List<UacGroupIpAddr> uacGroupIpAddrList = uacGroupIpAddrService.select(uacGroupIpAddr);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), uacGroupIpAddrList);
    }

    /**
     * 保存ip地址设置
     */
    @PostMapping(value = "/saveIpSegment")
    public Wrapper saveIpSegment(@RequestBody UacGroupIpSegment uacGroupIpSegment) {

        Integer result = uacGroupIpSegmentService.save(uacGroupIpSegment, getLoginAuthDto());

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 根据Id删除ip地址设置
     */
    @PostMapping(value = "/deleteIpSegmentById/{id}")
    public Wrapper deleteIpSegmentById(@PathVariable Long id) {

        Integer result = uacGroupIpSegmentService.deleteByKey(id);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 查询ip地址设置
     */
    @PostMapping(value = "/queryIpSegmentList")
    public Wrapper queryIpSegmentList() {

        UacGroupIpSegment uacGroupIpSegment = new UacGroupIpSegment();
        uacGroupIpSegment.setGroupId(getLoginAuthDto().getGroupId());
        List<UacGroupIpSegment> uacGroupIpSegmentList = uacGroupIpSegmentService.select(uacGroupIpSegment);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), uacGroupIpSegmentList);
    }

    /**
     * 保存ip地址设置
     */
    @PostMapping(value = "/saveIpUser")
    public Wrapper saveIpUser(@RequestBody UacGroupIpUser uacGroupIpUser) {

        Integer result = uacGroupIpUserService.save(uacGroupIpUser, getLoginAuthDto());

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 根据Id删除ip地址设置
     */
    @PostMapping(value = "/deleteIpUserById/{id}")
    public Wrapper deleteIpUserById(@PathVariable Long id) {

        Integer result = uacGroupIpUserService.deleteByKey(id);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 查询ip地址设置
     */
    @PostMapping(value = "/queryIpUserList")
    public Wrapper queryIpUserList() {

        UacGroupIpUser uacGroupIpUser = new UacGroupIpUser();
        uacGroupIpUser.setGroupId(getLoginAuthDto().getGroupId());
        List<UacGroupIpUser> uacGroupIpUserList = uacGroupIpUserService.select(uacGroupIpUser);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), uacGroupIpUserList);
    }


}
